package com.ucomm.talent.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import com.ucomm.talent.R;
import com.vita.bduck.common.StringUtils;
import com.ucomm.talent.widget.SearchSpinnerView;
import com.ucomm.talent.widget.SingleChoiceDialog;

import java.util.List;

/**
 * 搜索主界面
 * Created with IntelliJ IDEA.
 * User: Vita Hao
 * Date: 13-5-21
 */
public class SearchActivity extends BaseActivity implements ICustomSpinnerClickListener ,View.OnClickListener{
    private SingleChoiceDialog mSingleChoiceDialog;
    private Context mContext;
    List<String> prvinceList;
    List<String> updateTimeList;
    List<String> workExperienceList;
    List<String> cagetoryList;
    SearchSpinnerView spPrvince;
    SearchSpinnerView spCategory;
    SearchSpinnerView spUpdateTime;
    RadioButton rbFindJob;
    RadioButton rbFindTalent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.job_search_layout);
        initDate();
        initView();
    }

    private void initView() {

        //标签页
        rbFindJob = (RadioButton) findViewById(R.id.rb_find_job);
        rbFindJob.setChecked(true);
        rbFindTalent = (RadioButton) findViewById(R.id.rb_find_talent);


        //省份
        spPrvince = (SearchSpinnerView) findViewById(R.id.sp_prvince);
        spPrvince.setCustomSpinnerClickListener(spPrvince.getId(), this);
        if (prvinceList.size() > 0) {
            spPrvince.setDefaultSpinnerText(prvinceList.get(0));
        }
        //更新时间
        spUpdateTime = (SearchSpinnerView) findViewById(R.id.sp_update_time);
        spUpdateTime.setCustomSpinnerClickListener(spUpdateTime.getId(), this);

        if (updateTimeList.size() > 0) {
            spUpdateTime.setDefaultSpinnerText(updateTimeList.get(0));
        }

        //类别
        spCategory = (SearchSpinnerView) findViewById(R.id.sp_category);
        spCategory.setCustomSpinnerClickListener(spCategory.getId(), this);

        if (workExperienceList.size() > 0) {
            spCategory.setDefaultSpinnerText(cagetoryList.get(0));
        }


    }

    private void initDate() {
        prvinceList = StringUtils.stringArrayToList(getResources().getStringArray(R.array.province_list));//省份
        updateTimeList = StringUtils.stringArrayToList(getResources().getStringArray(R.array.update_time_list));//更新时间
        workExperienceList = StringUtils.stringArrayToList(getResources().getStringArray(R.array.work_experience));//工作经验
        cagetoryList = StringUtils.stringArrayToList(getResources().getStringArray(R.array.job_cagetory));
    }

    /**
     * 创建省份列表对话框
     */
    private void createProvinceDialog() {
        mSingleChoiceDialog = new SingleChoiceDialog(this, prvinceList);
        mSingleChoiceDialog.setTitle("请选择");
        mSingleChoiceDialog.setOnOKButtonListener(new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                int selItem = mSingleChoiceDialog.getSelectItem();
                if (prvinceList.size() > selItem) {
                    spPrvince.setDefaultSpinnerText(prvinceList.get(selItem));
                }
            }

        });
        mSingleChoiceDialog.show();
    }

    /**
     * 创建更新时间列表对话框
     */
    private void createspUpdateTimeDialog() {
        mSingleChoiceDialog = new SingleChoiceDialog(this, updateTimeList);
        mSingleChoiceDialog.setTitle("请选择");
        mSingleChoiceDialog.setOnOKButtonListener(new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                int selItem = mSingleChoiceDialog.getSelectItem();
                if (updateTimeList.size() > selItem) {
                    spUpdateTime.setDefaultSpinnerText(prvinceList.get(selItem));
                }
            }

        });
        mSingleChoiceDialog.show();
    }

    /**
     * 创建省份列表对话框
     */
    private void createCategoryDialog() {
        mSingleChoiceDialog = new SingleChoiceDialog(this,cagetoryList);
        mSingleChoiceDialog.setTitle("请选择");
        mSingleChoiceDialog.setOnOKButtonListener(new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                int selItem = mSingleChoiceDialog.getSelectItem();
                if (cagetoryList.size() > selItem) {
                    spCategory.setDefaultSpinnerText(cagetoryList.get(selItem));
                }
            }

        });
        mSingleChoiceDialog.show();
    }

    @Override
    public void onArrowClick(int id, View view) {
        if (id == spPrvince.getId()) {
            createProvinceDialog();
        } else if (id == spCategory.getId()) {
            createCategoryDialog();
        } else if (id == spUpdateTime.getId()) {
            createspUpdateTimeDialog();
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == rbFindJob.getId()){

        }else if(id == rbFindTalent.getId()){

        }

    }
}
